package com.itsu.application.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import com.itsu.application.entity.Message;
import com.itsu.application.entity.User;
import com.itsu.application.framework.session.SessionContext;
import com.itsu.application.framework.websocket.WebSocketComponent;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.ListView;
import javafx.scene.control.TextArea;
import javafx.scene.input.KeyEvent;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;

import java.io.File;
import java.nio.charset.Charset;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class TalkController {

    private static final Log log = LogFactory.get();

    @FXML
    private ListView talkRecent;
    @FXML
    private ListView myFrends;
    @FXML
    private ListView talkGroup;
    @FXML
    private Button subMsg;
    @FXML
    private TextArea sendText;
    @FXML
    private WebView showText;

    private String webViewContent;

    private String toUserNameId;

    public String getToUserNameId() {
        return toUserNameId;
    }

    public void setToUserNameId(String toUserNameId) {
        this.toUserNameId = toUserNameId;
    }

    public String getWebViewContent() {
        return webViewContent;
    }

    public void setWebViewContent(String webViewContent) {
        this.webViewContent = webViewContent;
    }

    // Event Listener on Button[#subMsg].onAction
    @FXML
    public void send(ActionEvent event) {
        if (StrUtil.isBlank(webViewContent)) {
            return;
        }
        String text = sendText.getText();
        if (StrUtil.isNotBlank(text)) {
            User loginUser = (User) SessionContext.getSession().getAttribute("loginUser");
            String h5Text = "<div style='text-align: right;'><div style='color: #25B541; font-size: 20px;'>"
                    + loginUser.getUserName() + "(" + loginUser.getUserId() + ")  "
                    + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")).toString()
                    + "</div>" + text + "</div>";
            String sourceShowText = webViewContent;
            final WebEngine engine = showText.getEngine();
            final String content = sourceShowText + "<br>" + h5Text;
            engine.loadContent(content);
            sendText.setText("");
            webViewContent = content;
            Message message = new Message();
            message.setFromUserId(loginUser.getUserId());
            message.setToUserId(StrUtil.sub(this.toUserNameId, StrUtil.indexOf(this.toUserNameId, '(') + 1, StrUtil.indexOf(this.toUserNameId, ')')));
            message.setContent(text);
            try {
                WebSocketComponent.sendMsg(message);
            } catch (Exception e) {
                log.error(e);
            }
            //TODO save local recent talk message
        }

    }

    public void saveRecentTalkMessage(Message message){
        String dateStr = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy.MM.dd"));
        File file = new File("storage/talk_recent_" + dateStr);
        JSONArray jsonArray = JSONUtil.readJSONArray(file, Charset.defaultCharset());

        JSONUtil.createObj();
    }

    // Event Listener on TextArea[#sendText].onKeyTyped
    @FXML
    public void onKeyTyped(KeyEvent event) {
        // TODO Autogenerated
    }

    public ListView getTalkRecent() {
        return talkRecent;
    }

    public void setTalkRecent(ListView talkRecent) {
        this.talkRecent = talkRecent;
    }

    public ListView getMyFrends() {
        return myFrends;
    }

    public void setMyFrends(ListView myFrends) {
        this.myFrends = myFrends;
    }

    public ListView getTalkGroup() {
        return talkGroup;
    }

    public void setTalkGroup(ListView talkGroup) {
        this.talkGroup = talkGroup;
    }

    public Button getSubMsg() {
        return subMsg;
    }

    public void setSubMsg(Button subMsg) {
        this.subMsg = subMsg;
    }

    public TextArea getSendText() {
        return sendText;
    }

    public void setSendText(TextArea sendText) {
        this.sendText = sendText;
    }

    public WebView getShowText() {
        return showText;
    }

    public void setShowText(WebView showText) {
        this.showText = showText;
    }

    public enum TalkType {
        Single, Group;
    }
}
